package ca.usherbrooke.domus.sipnmove2.model.communication.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;

/**
 * Handle a socket communication between two parties
 */
public class SocketServerCommunication extends AbstractSocketCommunication {

	/**
	 * Port to use
	 */
	private Integer port;
	
	/**
	 * Socket server
	 */
	private ServerSocket serverSocket;

	/**
	 * Default constructor
	 * @param ipAddress IP address
	 * @param port Port
	 */
	public SocketServerCommunication(Integer port) {
		
		this.port = port;
		
	}

	@Override
	public void initialize() {

		try {
			
			serverSocket = new ServerSocket(port);
			
		} catch (IOException e) {
			e.printStackTrace();
		}

		new Thread() {
			public void run() {
				
				try {
					
					socket = serverSocket.accept();
					
					outputBuffer = new PrintWriter(socket.getOutputStream(), true);
					inputBuffer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		}.start();
		
	}

}
